//
//  LCRecommendCollection.m
//  JKFuYinProject
//
//  Created by admin on 2018/4/17.
//  Copyright © 2018年 LC. All rights reserved.
//

#import "LCRecommendCollection.h"
#import "LCVideoClassifiesMoreVC.h"
#import "LCVideoSingleListsCollectionView.h"
#import "LCNewVideoListsViewCell.h"
#import "LCRecommendVideoClassifiesMoreVC.h"
#import "LCRecommendCollection+LoadData.h"

@interface LCRecommendCollection ()
{
    NSInteger toRefresh;
}
@end

@implementation LCRecommendCollection

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController.navigationBar setBarTintColor:[UIColor colorWithRed:0.89 green:0.32 blue:0.30 alpha:1.00]];
}
- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    NSLog(@"111111111");
//    if (toRefresh != 1) {
//        toRefresh = 1;
//        NSLog(@"%f>>>>>>%f",ScreenWidth,ScreenHeight);
//        NSLog(@"%f>>>>>>%f",self.view.frame.size.width,self.view.frame.size.height);
//
//        self.collectionView.frame = CGRectMake(0, 5, ScreenWidth, ScreenHeight- 44 - NAVIGATION_BAR_HEIGHT - TAB_BAR_HEIGHT - 5);
//        [self.collectionView reloadData];
//    }
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.type = @"0";
    [self setCollectionUI];
    [self getData];
    [LCUtils lcAddSideLeftView:self.view];
//    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//        LCVideoDetailChangeHeaderController *videoDetailChangeHeaderController = [[LCVideoDetailChangeHeaderController alloc] init];
////        videoDetailChangeHeaderController.listAboutModel = listAboutModel;
//        [self.navigationController pushViewController:videoDetailChangeHeaderController animated:YES];
//    });
}

#pragma mark <UICollectionViewDataSource>
//- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
//    return CGSizeMake(ScreenWidth,30+5);
//}
/// footer 大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section{
    if (section == self.videoManager.count - 1) {
        return CGSizeMake(ScreenWidth, 30.0);
    }
    return CGSizeZero;
}
#pragma mark - 设置有几个区
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return  self.videoManager.count;
}
#pragma mark - 设置每个区有几个
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 1;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    LCNewVideoListsViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    
    cell.labelTitle.text = NullStringjudgment(NSStr(self.videoManager[indexPath.section].name));
    [cell.labelTitle sizeToFit];
    
    cell.imageViewClickMove.tag = indexPath.section+10000;
    [cell.imageViewClickMove addTarget:self action:@selector(clickMove:) forControlEvents:UIControlEventTouchUpInside];

    LCRecommendHomeModel *model = self.videoManager[indexPath.section];
    @weakify(self)
    cell.myCollectionView.model = model;
    cell.myCollectionView.pushBlock = ^(LCVideoHomeListAboutModel *listAboutModel) {
        @strongify(self)
        LCVideoDetailChangeHeaderController *videoDetailChangeHeaderController = [[LCVideoDetailChangeHeaderController alloc] init];
        videoDetailChangeHeaderController.listAboutModel = listAboutModel;
        [self.navigationController pushViewController:videoDetailChangeHeaderController animated:YES];

        NSArray * arrayGetData = [[NSArray alloc]initWithContentsOfFile:[LSZSpecialPackaging recordTheBrowsingHistoryVideo]];
        for (NSDictionary * dic in arrayGetData) {
            NSString * name = NSStr(dic[@"id_id"]);
            if ([name isEqualToString:listAboutModel.id]) {
                return;
            }
        }
        //还没有添加过
        NSMutableArray * mutArrayAdd = [[NSMutableArray alloc]initWithArray:arrayGetData];
        NSMutableDictionary * mutDic = [[NSMutableDictionary alloc]initWithObjectsAndKeys:listAboutModel.name,@"title",listAboutModel.pic,@"imageViewUrl",listAboutModel.author,@"name",listAboutModel.id,@"id_id",nil];
        [mutArrayAdd addObject:mutDic];
        [mutArrayAdd writeToFile:[LSZSpecialPackaging recordTheBrowsingHistoryVideo] atomically:YES];
    };
    
    return cell;
}
//#pragma mark - 区头
//-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
//    UICollectionReusableView *reuseableView = nil;
//    // 如果是section的头
//    LSZVideoTwoHeaderCell * headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"cellHeader" forIndexPath:indexPath];
//    headerView.butMove.frame = CGRectMake(10, 10, ScreenWidth-20, 30);
//    headerView.labelTitle.text = NullStringjudgment(NSStr(self.videoManager[indexPath.section].name));
//    [headerView.labelTitle sizeToFit];
//    headerView.butMove.tag = indexPath.section+1;
//    [headerView.butMove addTarget:self action:@selector(clickMoveBut:) forControlEvents:UIControlEventTouchUpInside];
//    reuseableView = headerView;
//    return reuseableView;
//}
#pragma mark - 下面的分类的单个或者更多界面
-(void)clickMove:(UIButton *)but {
    if (but.tag-10000 < 3) {
        LCRecommendVideoClassifiesMoreVC *vc = [[LCRecommendVideoClassifiesMoreVC alloc]init];
        LCRecommendHomeModel *model = self.videoManager[but.tag-10000];
        vc.strTitle = NullStringjudgment(NSStr(model.name));
        vc.type = [NSString stringWithFormat:@"%zd",but.tag-10000+1];
        [self.navigationController pushViewController:vc animated:YES];
    }else{
        LCVideoClassifiesMoreVC * vc = [[LCVideoClassifiesMoreVC alloc]init];
        vc.style = @"2";
        LCRecommendHomeModel *model = self.videoManager[but.tag-10000];
        vc.strTitle = NullStringjudgment(NSStr(model.name));
        vc.type = model.type;
        [self.navigationController pushViewController:vc animated:YES];
    }
}
#pragma mark - cell大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat width = (self.view.width - 40 -1) / 3;
    return CGSizeMake(self.view.width - 20,width*1.44+20+80);
}
-(void)setCollectionUI {
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
//    flowLayout.itemSize = CGSizeMake(self.view.width - 20,width*1.44+20+80);
    flowLayout.sectionInset = UIEdgeInsetsMake(10, 5, 10, 5);
//        flowLayout.minimumLineSpacing = 10;//间距
    //    flowLayout.minimumInteritemSpacing = 10;
    self.collectionView = [[UICollectionView alloc]initWithFrame:self.view.frame collectionViewLayout:flowLayout];
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    self.view.backgroundColor = RGBColor(243, 243, 243);
    self.collectionView.backgroundColor = RGBColor(243, 243, 243);
    [self.view addSubview:self.collectionView];
    [self.collectionView registerClass:[LCNewVideoListsViewCell class] forCellWithReuseIdentifier:@"cell"];
    //    [self.collectionView registerClass:[LSZVideoTwoHeaderCell class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"cellHeader"];
    //    [self.collectionView registerClass:[LSZTabbarVideoOneHeaderCell class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"OneHeaderCell"];
    __weak typeof(self) weakSelf = self;
    self.collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [weakSelf getData];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [weakSelf.collectionView.mj_header endRefreshing];
        });
    }];
    [self setupCollectionViewLayout];

}

- (void)setupCollectionViewLayout
{
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.tabBarController.tabBar.translucent = NO;
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.and.right.equalTo(self.view);
        if (@available(iOS 11, *)) {
            make.top.equalTo(self.view.mas_safeAreaLayoutGuideTop);
            make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom).offset(-0);
        }else{
            make.top.equalTo(self.view.mas_top);
            make.bottom.equalTo(self.view.mas_bottom).offset(-0);
        }
    }];
}
#pragma mark - 缓存
-(void)getData
{
    [self loadTuijianData];
}

@end
